#include "client.h"

int tcpClientInit(char *ip, char *port, int *pSocketFd) {
    *pSocketFd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    int ret = inet_pton(AF_INET, ip, &serverAddr.sin_addr.s_addr);
    ERROR_CHECK(ret, -1, "inet_pton");
    serverAddr.sin_port = htons(atoi(port));
    ret = connect(*pSocketFd, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
    ERROR_CHECK(ret, -1, "connect");
    return 0;
}

int epollAdd(int epollFd, int fd) {
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = fd;
    int ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event);
    ERROR_CHECK(ret, -1, "EPOLL_CTL_ADD");
    return 0;
}

int epollDelete(int epollFd, int fd) {
    int ret = epoll_ctl(epollFd, EPOLL_CTL_DEL, fd, NULL);
    ERROR_CHECK(ret, -1, "EPOLL_CTL_DEL");
    return 0;
}
